

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ClassSearchResult extends JFrame{

    public ClassSearchResult(String userid, Object typeID) {

        SQLOperation sql = new SQLOperation();

        setTitle("Search Result");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel(typeID+" Information");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        String[] classAttributes={"typeID","tName"};
        Object[][] detailInfo=sql.search("DeviceType",classAttributes,"typeID",typeID);

        String[] deviceAttributes={"DeviceType_typeID","Family_familyID"};
        Object[][] deviceInfo=sql.search("Device",deviceAttributes,"DeviceType_typeID",typeID);

        int count=0;
        for(int i=0;i<deviceInfo.length;i++){
            if(deviceInfo[i][1].equals("")||deviceInfo[i][1].equals("0")) count++;
        }

        JPanel info=new JPanel(new GridLayout(3,1,5,5));
        for(int i=0;i<2;i++){
            info.add(new JLabel(classAttributes[i]+"  :  "+detailInfo[0][i]+"\n",JLabel.CENTER));
        }
        info.add(new JLabel("Device Type Numbers: "+count,JLabel.CENTER));

        add(info,BorderLayout.CENTER);




        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        JButton modify=new JButton("Modify it");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // MODIFY

                new ClassModify(userid,typeID);


            }
        });

        JButton delete=new JButton("Delete it");
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int deleteOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm Delete?","Confirm",JOptionPane.YES_NO_OPTION);

                if(deleteOperation==JOptionPane.YES_OPTION){

                    sql.modify("DeviceType","typeID",typeID,"shanchu",0);

                    sql.modify("Device","DeviceType_typeID",typeID,
                            "DeviceType_typeID","0");

                    JOptionPane.showMessageDialog
                            (null, "Delete Complete!","Success",JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);


                }




            }
        });
        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(modify);
        buttons.add(delete);

        add(buttons,BorderLayout.SOUTH);






        setSize(500,300);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
